﻿#pragma once
#include "HTable.h"

namespace hzp {
	template<class K, class V, class hash = Hash<K>>
	class My_Unordered_Map {
		struct MapKeyOfT {
			const K& operator()(const pair<K,V>& key) {
				return key.first;
			}
		};
	public:
		typedef typename openHash::HashBucket<K, pair<K, V>, MapKeyOfT, hash>::iterator iterator;
		iterator begin() {
			return hb.begin();
		}
		iterator end() {
			return hb.end();
		}
		V& operator[](const K& key) {
			auto ret = hb.Insert(make_pair(key, V()));
			return ret.first->second;
		}
		pair<iterator,bool> insert(const pair<K, V>& kv) {
			return hb.Insert(kv);
		}

	private:
		openHash::HashBucket<K, pair<K, V>, MapKeyOfT, hash> hb;
	};
	void test_map() {
		My_Unordered_Map<string, string> dict;
		dict.insert(make_pair("sort", "排序"));
		dict.insert(make_pair("string", "ַ字符串"));
		dict.insert(make_pair("map", "地图"));

		dict["sort"] = "排序1";
		cout << dict["sort"] << endl;
		My_Unordered_Map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;

		My_Unordered_Map<string, string> copy(dict);
		for (auto& e : copy) {
			cout << e.first << ":" << e.second << endl;
		}
	}
}